//=========================================引脚接线================================================//
// OLED模块               STM32F103C单片机
//   VCC         接       DC 5V/3.3V      //OLED屏电源正
//   GND         接          GND          //OLED屏电源地
//=======================================液晶屏数据线接线==========================================//
//   D1          接          PA7        //OLED屏SPI写信号
//   D0          接          PA5        //OLED屏SPI时钟信号
//=======================================液晶屏控制线接线==========================================//
//   RES         接          PA8        //OLED屏复位控制信号
//   DC          接          PA9        //OLED屏数据/命令选择控制信号
//   CS          接          PA4        //OLED屏片选控制信号
//本oled模块不带触摸功能，所以不需要触摸屏接线

#include "stm32f10x.h"
#include "Hardware/Delay.h"
#include "Hardware/OLED.h"
#include "Hardware/LED.h"
#include "Hardware/bmp.h"

//比较笼统的fps统计
void TIM2_Configuration(void);
void TIM2_IRQHandler(void);
// 帧率计数器(比较笼统)相关的变量
uint32_t msCount = 0;
uint32_t fps = 0;
uint32_t fps_count = 0;

//主函数
int main(void)
{
	  //点个指示灯
		LED_Init();
		LED1_ON();
	
		//OLED_SPI初始化
    OLED_Init();
	
		//设置定时器中断
		TIM2_Configuration();
		
		while(1){
		//显示字符
			OLED_ZFC(0, 7, (uint8_t *)"A", Font_Size_F6X8);
			
			//显示汉字
			OLED_ONE_HZ(65, 3, 1, 2);
			OLED_ONE_HZ(80, 3, 1, 1);
			OLED_ONE_HZ(95, 3, 1, 0);

			// 在OLED屏幕的X轴为4，Y轴为7，显示6*8大小的字符串
			OLED_ZFC(45, 6, (uint8_t *)"FPS:", Font_Size_F8x16);
		
			//显示图片(二极管图标)
			OLED_ShowPicture(0, 0, 32, 4, dinosaurBMP1);
			
			//显示图片(二极管图标)
			OLED_ShowPicture(0, 0, 32, 4, dinosaurBMP2);
			//累加帧数
			fps_count++;
		}
		
    //OLED_Clear(0); // 清屏
			
}


//一毫秒中断
void TIM2_IRQHandler(void)
{
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
    {
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
        msCount++;

			  if(msCount >= 1000){ //累计1秒 就清空以及显示帧数
					  msCount = 0;
					  fps = fps_count;
					  fps_count = 0;
						
						//显示帧数
						OLED_ShowNum(80, 6, fps, Font_Size_F8x16);
				}
    }
}

//初始化定时器
void TIM2_Configuration(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

    TIM_TimeBaseStructure.TIM_Period = 7200 - 1; // 72MHz / 7200 = 10kHz
    TIM_TimeBaseStructure.TIM_Prescaler = 10 - 1; // 10kHz / 10 = 1kHz (1ms)
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

    TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM2, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
 